/*
 * MainFrameCompiler.java
 *
 * Created on 30 de Março de 2010, 17:25
 */

package gui;

import app.Code;
import app.Lexico;
import app.Token;
import com.anlugifa.editor.ui.JEditor;
import com.nilo.plaf.nimrod.NimRODLookAndFeel;
import com.nilo.plaf.nimrod.NimRODTheme;
import exceptions.LexicoException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.UIManager;

/**
 *
 * @author  User
 */
public class MainFrameCompiler extends javax.swing.JFrame {
    
    private JEditor editor;
    private Action actionCompile = new CompileAction();
    private static MainFrameCompiler instance;
    
    /**
     * Silgleton que retorna a instancia da GUI principal do projeto
     * @return MainFrameCompiler
     */
    public static MainFrameCompiler getInstance(){
        if(instance == null){
            instance = new MainFrameCompiler();
        }
        
        return instance;
    }
    
    /**
     * Metodo para adicionar logs de mensagens na interface gráfica
     * @param String
     */
    public void addErrorMessage(String message){
        this.jTextAreaCompileOut.setText(message);
        this.jTextAreaCompileOut.setForeground(Color.RED);
    }
    
    protected void compile() throws FileNotFoundException, IOException, LexicoException{
        File file = editor.getFile();
        
        if(file != null){
            try {
                Lexico lexico = new Lexico(file.getAbsolutePath());

                LinkedList<Token> list = lexico.getList();

                String out = "";

                for(Token t : list){
                    out += t.getSimbol() + 
                        "\t\t" + Code.getGenericDescrition(t.getCode()) +
                        "\t\t" + t.getLine() + "\n";
                }

                this.jTextAreaCompileOut.setForeground(Color.BLACK);
                this.jTextAreaCompileOut.setText(out);
                System.out.println(out);

            } catch (Exception ex) {
                ex.printStackTrace();
            } 
        }
        else{
            try {
                File arqv = new File("./temp.jj");

                BufferedWriter buffer = new BufferedWriter(new FileWriter(arqv));
                String text = editor.getTextPane().getText();
                buffer.write(text);
                buffer.flush();
                buffer.close();

                Lexico lexico = new Lexico(arqv.getAbsolutePath());

                LinkedList<Token> list = lexico.getList();

                String out = "";

                for(Token t : list){
                    out += t.getSimbol() +
                        "\t\t" + Code.getGenericDescrition(t.getCode()) +
                        "\t\t" + t.getLine() + "\n";
                }

                this.jTextAreaCompileOut.setForeground(Color.BLACK);
                this.jTextAreaCompileOut.setText(out);
                System.out.println(out);

            } catch (Exception ex) {
                ex.printStackTrace();
            } 
        }
    }
    
    private class CompileAction extends AbstractAction{
        
        public CompileAction(){
            super("compile");
        }
        
        public void actionPerformed(ActionEvent e) {
            try {
                compile();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MainFrameCompiler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MainFrameCompiler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (LexicoException ex) {
                Logger.getLogger(MainFrameCompiler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public MainFrameCompiler() {
        initComponents();
        
        try{
            editor = JEditor.getInstance();
            this.setJMenuBar(editor.getMnuBar());
            this.setLayout(new BorderLayout());
            this.add(BorderLayout.NORTH, editor.createToolbar());
            editor.addButton(actionCompile);
            this.jPanel1.setLayout(new BorderLayout());
            this.jPanel1.add(BorderLayout.CENTER, editor.getPnlText());
            this.setTitle("Compilador JJ");
        }catch(Exception e){
            
        }
        
        this.setLocationRelativeTo(null);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaCompileOut = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 657, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 356, Short.MAX_VALUE)
        );

        jTextAreaCompileOut.setColumns(20);
        jTextAreaCompileOut.setEditable(false);
        jTextAreaCompileOut.setRows(5);
        jTextAreaCompileOut.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Out", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0)));
        jScrollPane1.setViewportView(jTextAreaCompileOut);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            NimRODTheme nt = new NimRODTheme();
            nt.setPrimary1(new Color(84, 84, 192));
            nt.setPrimary2(new Color(94, 94, 202));
            nt.setPrimary3(new Color(104, 104, 212));
            nt.setSecondary1(new Color(25, 30, 39));
            nt.setSecondary2(new Color(35, 40, 49));
            nt.setSecondary3(new Color(45, 50, 59));
            nt.setBlack(new Color(0, 0, 0));
            nt.setWhite(new Color(255, 255, 255));
            nt.setOpacity(195);
            nt.setFrameOpacity(180);
            NimRODLookAndFeel NimRODLF = new NimRODLookAndFeel();
            NimRODLF.setCurrentTheme(nt);
            UIManager.setLookAndFeel(NimRODLF);
            java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                    MainFrameCompiler.getInstance().setVisible(true);
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaCompileOut;
    // End of variables declaration//GEN-END:variables
    
}
